//
//  WBVisitorView.swift
//  XLWB
//
//  Created by MacBook on 16/6/30.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    //注册
    lazy var registerBtn: UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange(), highlightedColor: UIColor.black(), backgroundImageName: "common_button_white_disable")
    
    //登录
    lazy var loginBtn: UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray(), highlightedColor: UIColor.black(), backgroundImageName: "common_button_white_disable")

    
    //访客视图的信息字典
    //如果是首页 imageName == ""
    var visitorInfo: [String: String]? {
    
        didSet {
        
            //取字典信息
            guard let imageName = visitorInfo?["imageName"],
                message = visitorInfo?["message"] else {
                    
                    return
        
            }
            
            //设置消息
            tipLable.text = message
            //设置图像,首页不需要设置
            if imageName == "" {
                
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)

            //其他控制器的视图不需要显示小房子和遮罩视图
            houseView.isHidden = true
            maskIconView.isHidden = true
        }
    
    
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //旋转图标动画
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
    
        //动画完成不删除,如果iconView被释放,动画一起销毁
        //连续播放动画
        anim.isRemovedOnCompletion = false
        
        iconView.layer.add(anim, forKey: nil)
    
    
    }
    
        
    //MARK--私有属性
    //懒加载属性只有电泳UIKit控件指定的构造函数,其他都需要使用类型
    //环形视图
    private lazy  var iconView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    
    //标签
    private lazy var tipLable: UILabel = UILabel .cz_label(
        withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray()
    )
    
        //遮罩视图
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}

//MARK--设置界面
extension WBVisitorView {

    func setupUI () {
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        addSubview(iconView)
        addSubview(houseView)
        addSubview(tipLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        addSubview(maskIconView)
        
        
        tipLable.textAlignment = .center
        //取消autoresizing
        for v in subviews {
        
            v.translatesAutoresizingMaskIntoConstraints = false
        
        }
        
        //自动布局
        let margin:CGFloat = 20.0
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        
        //小房子布局
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        //标签
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        //注册
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registerBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))

        //登录
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerBtn,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //遮罩
        let viewDict = ["maskIconView": maskIconView,
                        "registerBtn": registerBtn]
        let metrics = ["spacing": 35]
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registerBtn]",
            options: [],
            metrics: metrics,
            views: viewDict))



    }



}
